﻿Imports BUS
Imports DTO

Public Class MH_CapNhatDiemTheoLop_Mon
    Dim old As Double
    Private Sub MH_CapNhatDiemTheoLop_Mon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Lay danh sach lop
        Dim lBus As New LopBUS()
        Dim dtLop As DataTable = lBus.LayBang()
        cboLop.DataSource = dtLop
        cboLop.DisplayMember = "TenLop"
        cboLop.ValueMember = "MaLop"

        'Lay danh sach mon
        Dim mBus As New MonHocBUS()
        Dim dtMonHoc As DataTable = mBus.LayBang()
        cboMon.DataSource = dtMonHoc
        cboMon.DisplayMember = "TenMon"
        cboMon.ValueMember = "MaMon"

        'lay danh sach hoc ky
        Dim hkBus As New HocKyBUS()
        Dim dtHocKy As DataTable = hkBus.LayBang()
        cboHocKy.DataSource = dtHocKy
        cboHocKy.DisplayMember = "HocKy"
        cboHocKy.ValueMember = "MaHocKy"

    End Sub

    Private Sub btnLayBangDiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLayBangDiem.Click

        'lay gia tri tu cac button
        Dim MaHocKy As Integer = cboHocKy.SelectedValue
        Dim MaMonHoc As Integer = cboMon.SelectedValue
        Dim MaLop As Integer = cboLop.SelectedValue

        Dim str = "SELECT HOCSINH.HOTEN, CHITIETDIEM.DIEM15P, CHITIETDIEM.DIEM1T, CHITIETDIEM.DIEMHK, CHITIETDIEM.MAHOCSINH, CHITIETDIEM.MADIEMMONHOC, CHITIETDIEM.MACHITIETDIEM FROM HOCSINH, DIEMMONHOC, CHITIETDIEM WHERE HOCSINH.MAHOCSINH = CHITIETDIEM.MAHOCSINH AND HOCSINH.MALOP = DIEMMONHOC.MALOP And DIEMMONHOC.MADIEMMONHOC = CHITIETDIEM.MADIEMMONHOC"
        str += " AND DIEMMONHOC.MALOP = " + MaLop.ToString
        str += " AND DIEMMONHOC.MAMON = " + MaMonHoc.ToString
        str += " AND DIEMMONHOC.MAHOCKY = " + MaHocKy.ToString
        str += " GROUP BY HOCSINH.HOTEN, CHITIETDIEM.DIEM15P, CHITIETDIEM.DIEM1T, CHITIETDIEM.DIEMHK, CHITIETDIEM.MAHOCSINH, CHITIETDIEM.MADIEMMONHOC, CHITIETDIEM.MACHITIETDIEM "


        Dim DBBangDiem As New DataTable
        Dim busTemp As New ThuVienChungBUS
        DBBangDiem = busTemp.LayBang(str)

        'clear datagridviewcu
        dgvDanhSach.Rows.Clear()
        'duyet danh sach va gan vao datagridview
        For i As Integer = 0 To DBBangDiem.Rows.Count - 1
            dgvDanhSach.Rows.Add(i + 1, DBBangDiem.Rows(i)("HOTEN"), DBBangDiem.Rows(i)("DIEM15P"), DBBangDiem.Rows(i)("DIEM1T"), DBBangDiem.Rows(i)("DIEMHK"), DBBangDiem.Rows(i)("MAHOCSINH"), DBBangDiem.Rows(i)("MADIEMMONHOC"), DBBangDiem.Rows(i)("MACHITIETDIEM"))
        Next

    End Sub

    Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
        Dim dsBangDiem As New List(Of BangDiemDTO)
        For i As Integer = 0 To dgvDanhSach.Rows.Count - 1
            Dim bd As New BangDiemDTO
            bd.MaChiTietDiem = dgvDanhSach.Rows(i).Cells("MaChiTietDiem").Value
            bd.Diem15P = dgvDanhSach.Rows(i).Cells("clDIEM15P").Value
            bd.Diem1T = dgvDanhSach.Rows(i).Cells("clDIEM1T").Value
            bd.DiemHK = dgvDanhSach.Rows(i).Cells("clDIEMCUOIHK").Value
            dsBangDiem.Add(bd)
        Next
        Dim busTemp As New BangDiemBUS
        busTemp.CapNhatBangDiem(dsBangDiem)
    End Sub

    Private Sub dgvDanhSach_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDanhSach.CellValueChanged
        Try
            Dim cell As DataGridViewCell = dgvDanhSach.CurrentCell
            If (cell.Value < 0 Or cell.Value > 10) Then
                MessageBox.Show("Điểm chỉ dc từ 0 đến 10")
                cell.Value = old
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub dgvDanhSach_CellMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvDanhSach.CellMouseDoubleClick
        Try
            old = dgvDanhSach.CurrentCell.Value
        Catch ex As Exception

        End Try

    End Sub

    Private Sub dgvDanhSach_CellMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvDanhSach.CellMouseClick
        Try
            old = dgvDanhSach.CurrentCell.Value
        Catch ex As Exception

        End Try
    End Sub
End Class